#include "BaseMainWidget.h"

#include <QApplication>
#include <QString>
#include <QSize>
#include <QPainter>

namespace UserManagementSystem {
BaseMainWidget::BaseMainWidget(QWidget* parent)
:FramelessWidget(parent){

    qDebug()<<__FUNCTION__;
    //最大化显示
    this->showMaximized();


    //初始化窗口
    m_settingWidget=new SettingWidget(this);


    //初始化控件
    m_mainLayout=new QVBoxLayout(this);
    m_subLayout=new QHBoxLayout();
    m_titleLabel=new QLabel();
    m_netStatusBtn=new QPushButton();
    m_settingBtn=new QPushButton();
    m_clinicInfoBtn=new QPushButton();
    m_logOutBtn=new QPushButton();
    m_vLine=new QFrame();
    m_minimumBtn=new QPushButton();
    m_closeBtn=new QPushButton();

    //初始化控件属性
    InitDefaultLayout();

    //绑定信号槽函数
    BindSlots();

}

QVBoxLayout *BaseMainWidget::GetMainLayout() const
{
    if(m_mainLayout){
        return m_mainLayout;
    }
    return nullptr;
}

void BaseMainWidget::On_CloseBtn_clicked()
{
    this->close();
}

void BaseMainWidget::On_MinimumBtn_clicked()
{

    this->showMinimized();
}

void BaseMainWidget::On_SettingBtn_clicked()
{
    qDebug()<<"点击了设置按钮";
    m_settingWidget->show();
}

void BaseMainWidget::InitDefaultLayout()
{
    m_subLayout->addWidget(m_titleLabel);
    m_subLayout->addStretch(1);
    m_subLayout->addWidget(m_netStatusBtn);
    m_subLayout->addSpacing(10);
    m_subLayout->addWidget(m_settingBtn);
    m_subLayout->addSpacing(10);
    m_subLayout->addWidget(m_clinicInfoBtn);
    m_subLayout->addSpacing(10);
    m_subLayout->addWidget(m_logOutBtn);
    m_subLayout->addSpacing(10);
    m_subLayout->addWidget(m_vLine);
    m_subLayout->addSpacing(10);
    m_subLayout->addWidget(m_minimumBtn);
    m_subLayout->addSpacing(10);
    m_subLayout->addWidget(m_closeBtn);

    m_mainLayout->addLayout(m_subLayout);

    //在标题栏下方添加一个弹簧，将标题压倒顶部
    m_mainLayout->setContentsMargins(20,30,20,20);

    m_mainLayout->setSpacing(20);

    InitDefaultStyle();
}

void BaseMainWidget::InitDefaultStyle()
{




    m_netStatusBtn->setFixedSize(25,25);
    m_settingBtn->setFixedSize(25,25);
    m_clinicInfoBtn->setFixedSize(25,25);
    m_logOutBtn->setFixedSize(25,25);
    m_minimumBtn->setFixedSize(25,25);
    m_closeBtn->setFixedSize(25,25);


    m_vLine->setFrameShape(QFrame::VLine);
    m_vLine->setLineWidth(1);


    //邮箱图标
    QString title_path=QApplication::applicationDirPath()+"/res/PatientManager/cameo.png";

    QString online_path=QApplication::applicationDirPath()+"/res/PatientManager/online.png";
    QString online_path_hover=QApplication::applicationDirPath()+"/res/PatientManager/online_hover.png";

    QString setting_path=QApplication::applicationDirPath()+"/res/PatientManager/setting.png";
    QString setting_path_hover=QApplication::applicationDirPath()+"/res/PatientManager/setting_hover.png";

    QString clinic_path=QApplication::applicationDirPath()+"/res/PatientManager/account.png";
    QString clinic_path_hover=QApplication::applicationDirPath()+"/res/PatientManager/account_hover.png";

    QString logout_path=QApplication::applicationDirPath()+"/res/PatientManager/logout.png";
    QString logout_path_hover=QApplication::applicationDirPath()+"/res/PatientManager/logout_hover.png";

    QString min_path=QApplication::applicationDirPath()+"/res/PatientManager/min.png";
    QString min_path_hover=QApplication::applicationDirPath()+"/res/PatientManager/min_hover.png";

    QString close_path=QApplication::applicationDirPath()+"/res/PatientManager/close.png";
    QString close_path_hover=QApplication::applicationDirPath()+"/res/PatientManager/close_hover.png";

    QPixmap pix(title_path);
    pix=pix.scaled(100,25);
    m_titleLabel->setPixmap(pix);
    m_titleLabel->adjustSize();



    this->LoadIcon(m_netStatusBtn,online_path,online_path_hover);
    this->LoadIcon(m_settingBtn,setting_path,setting_path_hover);
    this->LoadIcon(m_clinicInfoBtn,clinic_path,clinic_path_hover);
    this->LoadIcon(m_logOutBtn,logout_path,logout_path_hover);
    this->LoadIcon(m_minimumBtn,min_path,min_path_hover);
    this->LoadIcon(m_closeBtn,close_path,close_path_hover);

}

void BaseMainWidget::BindSlots()
{
    connect(m_closeBtn,&QPushButton::clicked,this,&BaseMainWidget::On_CloseBtn_clicked);
    connect(m_minimumBtn,&QPushButton::clicked,this,&BaseMainWidget::On_MinimumBtn_clicked);
    connect(m_settingBtn,&QPushButton::clicked,this,&BaseMainWidget::On_SettingBtn_clicked);
}

void BaseMainWidget::LoadIcon(QPushButton* btn,const QString path_1,const QString path_2)
{
    QString style=QString(R"(
    QPushButton {
        border: none;
        image: url(%1);
    }
    QPushButton:hover {
        image: url(%2);
    }
)").arg(path_1).arg(path_2);

    btn->setStyleSheet(style);

}

void BaseMainWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    painter.setRenderHint(QPainter::Antialiasing);

    QColor blue(255,255,255,255);//带透明度的蓝色
    painter.setBrush(blue);
    painter.setPen(Qt::NoPen);
    painter.drawRect(this->rect());

}

}
